public with sharing class AptiIntegration_DataMap {
	//Opportunity id prefix 
	private static string objectPrefix = '006';
	
	@future(callout=true)
	public static void doCallOutAptiService(set<string> aptiIdStatusDetail) {
		List<Integration_Log__c> lst_IntegrationLog = new List<Integration_Log__c>();
		
		for(string s : aptiIdStatusDetail) {
			apti_Request_Element req_obj = new apti_Request_Element();
			req_obj.RequestId = s.substring(0,s.indexOf('-'));
			req_obj.AptiId = s.substring(s.indexOf('-')+1, s.indexOf('+'));
			req_obj.StatusDetail = s.substring(s.indexOf('+')+1);
			String ServiceResponse;
            String exceptionstring;
			try {
                //if(docall)
                ServiceResponse = AptiIntegration_DataMap.doCallAptiSendMethod(req_obj);
                system.debug(ServiceResponse);  
            }
            catch(Exception ex) {
               exceptionstring = ex.getmessage();
               system.debug(exceptionstring);
            }
            finally {
            	if(ServiceResponse != null) {
            		if(req_obj.RequestId.startsWith(AptiIntegration_DataMap.objectPrefix))
            			lst_IntegrationLog.add(new Integration_Log__c(Opportunity__c = req_obj.RequestId,Status__c='Success',Request__c = String.valueOf(req_OBJ),Response__c = ServiceResponse));
            		else
            			lst_IntegrationLog.add(new Integration_Log__c(Lead__c = req_obj.RequestId,Status__c='Success',Request__c = String.valueOf(req_OBJ),Response__c = ServiceResponse));
            	}
            	else {
            		if(req_obj.RequestId.startsWith(AptiIntegration_DataMap.objectPrefix))
            			lst_IntegrationLog.add(new Integration_Log__c(Opportunity__c = req_obj.RequestId,Status__c='Failed',Request__c = String.valueOf(req_OBJ),Response__c = ServiceResponse, Detail__c=' Serer is not Responding . Returning ZERO'+exceptionstring));
            		else
            			lst_IntegrationLog.add(new Integration_Log__c(Lead__c = req_obj.RequestId,Status__c='Success',Request__c = String.valueOf(req_OBJ),Response__c = ServiceResponse));
            	}            	
            }
            
            if(lst_IntegrationLog.size()>0) {
                 insert lst_IntegrationLog;
            }
		}		
	} 
	
	public class apti_Request_Element {
		string RequestId {get; set; }
		string AptiId {get; set;}
		string StatusDetail {get; set;}
	}
	
	public static string doCallAptiSendMethod(apti_Request_Element senddata) {
		AptiIntegration.AptiServicesRest rest_Obj = new AptiIntegration.AptiServicesRest();
		return rest_Obj.send(sendData.RequestId,sendData.AptiId,sendData.StatusDetail);
	}
}